package com.ruoyi.service.impl;

import java.util.*;

import com.ruoyi.domain.ZhwyBuilding;
import com.ruoyi.domain.ZhwyDowntown;
import com.ruoyi.mapper.ZhwyBuildingMapper;
import com.ruoyi.mapper.ZhwyDowntownMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyShopsMapper;
import com.ruoyi.domain.ZhwyShops;
import com.ruoyi.service.IZhwyShopsService;

/**
 * 商铺信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class ZhwyShopsServiceImpl implements IZhwyShopsService {
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;
    @Autowired
    private ZhwyDowntownMapper zhwyDowntownMapper;
    @Autowired
    private ZhwyBuildingMapper zhwyBuildingMapper;


    /**
     * 查询商铺信息
     *
     * @param id 商铺信息主键
     * @return 商铺信息
     */
    @Override
    public ZhwyShops selectZhwyShopsById(Long id) {
        return zhwyShopsMapper.selectZhwyShopsById(id);
    }

    /**
     * 查询商铺信息列表
     *
     * @param zhwyShops 商铺信息
     * @return 商铺信息
     */
    @Override
    public List<ZhwyShops> selectZhwyShopsList(ZhwyShops zhwyShops) {
        List<ZhwyShops> list = zhwyShopsMapper.selectZhwyShopsList(zhwyShops);

        for (ZhwyShops zhs : list) {
            ZhwyDowntown zhwyDowntown = zhwyDowntownMapper.selectZhwyDowntownById(zhs.getDowntownId());
            ZhwyBuilding zhwyBuilding = zhwyBuildingMapper.selectZhwyBuildingById(zhs.getBuildingId());
            if (zhwyDowntown != null) {
                zhs.setDowntownName(zhwyDowntown.getName());
            }
            if (zhwyBuilding != null) {
                zhs.setBuildingName(zhwyBuilding.getName());
            }
        }
        return list;
    }

//    public Map selectZhwyShopsListMap(ZhwyShops zhwyShops) {
//        Map map = new HashMap<>();
//        List floors = new ArrayList<>();
//        List floorsList = new ArrayList<>();
//        List<ZhwyShops> list = selectZhwyShopsList(zhwyShops);
//        for (ZhwyShops zh : list) {
//            for (Object floor : floors) {
//                if (floor != zh.getFloor()) {
//                    floors.add(zh.getFloor());
//                }
//            }
//        }
//
//        for (Object floor : floors) {
//            for (ZhwyShops zhshops : list) {
//                if (floor == zhshops.getFloor()) {
//                    floorsList.add(zhshops);
//                }
//            }
//            map.put(floor,floorsList);
//            floorsList=new ArrayList<>();
//        }
//
//        return map;
//    }


    /**
     * 新增商铺信息
     *
     * @param zhwyShops 商铺信息
     * @return 结果
     */
    @Override
    public int insertZhwyShops(ZhwyShops zhwyShops) {
        ZhwyDowntown zhwyDowntown = zhwyDowntownMapper.selectZhwyDowntownById(zhwyShops.getDowntownId());
        ZhwyBuilding zhwyBuilding = zhwyBuildingMapper.selectZhwyBuildingById(zhwyShops.getBuildingId());
        if (zhwyDowntown != null && zhwyBuilding != null) {
            zhwyShops.setCode(zhwyDowntown.getId() + "-" + zhwyBuilding.getCode() + "-" + zhwyShops.getFloor() + "-" + zhwyShops.getRoom());
        }
        System.out.println(zhwyShops.getCode());
        return zhwyShopsMapper.insertZhwyShops(zhwyShops);
    }

    /**
     * 修改商铺信息
     *
     * @param zhwyShops 商铺信息
     * @return 结果
     */
    @Override
    public int updateZhwyShops(ZhwyShops zhwyShops) {
        return zhwyShopsMapper.updateZhwyShops(zhwyShops);
    }

    /**
     * 批量删除商铺信息
     *
     * @param ids 需要删除的商铺信息主键
     * @return 结果
     */
    @Override
    public int deleteZhwyShopsByIds(Long[] ids) {
        return zhwyShopsMapper.deleteZhwyShopsByIds(ids);
    }

    /**
     * 删除商铺信息信息
     *
     * @param id 商铺信息主键
     * @return 结果
     */
    @Override
    public int deleteZhwyShopsById(Long id) {
        return zhwyShopsMapper.deleteZhwyShopsById(id);
    }

    @Override
    public List<ZhwyShops> selectByDid(Long id) {
        List<ZhwyShops> list = zhwyShopsMapper.selectByDid(id);
        return list;
    }

    @Override
    public List selectByDidAndCode(Long id, String code) {
        List<ZhwyShops> list = zhwyShopsMapper.selectByDidAndCode(id, code);
        return list;
    }

    @Override
    public int selectState(int id) {
        int i = zhwyShopsMapper.selectOne(id);
        return i;
    }

    //    根据did和bid查询楼层
    @Override
    public List selectDBid(Long did, Long bid) {
        List<Integer> list = zhwyShopsMapper.selectDBid(did, bid);
        return list;
    }

}
